const app = getApp();
Page({
  // mixins: [require('../../mixin/themeChanged')],
  /**
   * 页面的初始数据
   */
  data: {
    hasUserInfo: false,
    userInfo: null,
  },
  onLoad: function() {
    // 页面加载时使用用户授权逻辑，弹出确认的框  
    this.userAuthorized()
    console.log("ssssss")
  },
  
  userAuthorized() {
    wx.getSetting({
      success: data => {
        if (data.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: data => {
              this.setData({
                hasUserInfo: true,
                userInfo: data.userInfo
              })
            }
          })
        } else {
          this.setData({
            hasUserInfo: false
          })
        }
      }
    })
  },
 onCheck(e){
    console.log("游客展示")
    const userInfo = e.detail.userInfo;
    this.setData({
      userInfo: userInfo
    })
    wx.switchTab({url: '../../pages/home/home'})
 },
  onGetUserInfo(e) {
    var that = this;
    console.log("进入了")
    const userInfo = e.detail.userInfo
    if (userInfo) {
      console.log("进入了1")
      // 1. 小程序通过wx.login()获取code
      wx.login({
        success: function(login_res) {
          //获取用户信息
          console.log("以下是code")
          console.log(login_res.code)
          wx.getUserInfo({
            success: function(info_res) {
              // 2. 小程序通过wx.request()发送code到开发者服务器
              wx.request({
              url:'http://47.94.215.148:8080/weiChe/authentication',
              
                method: 'POST',
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                data: {
                  js_code:login_res.code
                 
                },
                success: function(res) {
                 console.log(res)
                  if (res.data.status == 1||res.data.status == 3) {
                    // 7.小程序存储skey（自定义登录状态）到本地
                    wx.setStorageSync('userInfo', userInfo);
                    wx.setStorageSync('skey', res.data.data);
                   
                    getApp().globalData.isSetInfo = true,
                    
                    getApp().globalData.checked = 1;
                    if(res.data.status != 3){
                      getApp().globalData.inspector_openid=res.data.openId.openId,
                      getApp().globalData.username=res.data.openId.username,
                      getApp().globalData.password=res.data.openId.password,
                      getApp().globalData.isPost = true
                    }else{
                      getApp().globalData.isPost = false,
                      getApp().globalData.inspector_openid = res.data.openId
                    }
                    console.log("全局检查人员open_id"+getApp().globalData.inspector_openid)
                    wx.switchTab({
                      url: '../../pages/home/home',
                    })
                  } 
                  else
                  {
                    console.log('服务器异常');
                  }
                },
                fail: function(error) {
                  //调用服务端登录接口失败
                  console.log(error);
                }
              })
            }
          })
        }
      })
      this.setData({
        hasUserInfo: true,
        userInfo: userInfo,
        isSetInfo:app.globalData.isSetInfo
      })
    }
  }
 
})
 